from wsgiref.simple_server import make_server

def application(environ, start_response):
    """
    environ 是请求过来的数据
    start_response 设置请求头
    """
    if environ['PATH_INFO'] == "/favicon.ico":
        start_response('404 not found', [("Content-Type", "text/plain; version=0.0.4; charset=utf-8")])
        return ["sorry, there have no ico".encode("utf-8")]
    start_response('200 OK', [("Content-Type", "text/plain; version=0.0.4; charset=utf-8")])
    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0
    # u"获取表单数据"
    request_body = environ['wsgi.input'].read(request_body_size)
    print(request_body.decode("utf-8"))

    # u"这里我们使用PATH_INFO变量获取请求的URL, QUERY_STRING"
    body = 'Hello, %s!' % (environ['QUERY_STRING'] or 'web')
    return [body.encode('utf-8')]

# u"创建一个服务器，IP地址为空，端口是8000，处理函数是application:"
httpd = make_server('', 8000, application)
print('Serving HTTP on port 8000...')
# u"开始监听HTTP请求:"
httpd.serve_forever()
